const app = getApp();
const req = require("../../utils/http.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    username:'',
  },

  formSubmit(e){
    let { username, password } = e.detail.value
    if (!username || !password){
      wx.showToast({
        title: '请输入账号名或密码',
        icon:'none',
        duration:800
      })
      return;
    }

    let params ={
      username,
      password
    }
    req.loginHttp('/sys/sysUser/login','POST',params).then(res=>{
      if(res.data.code==200){
        let result = res.data.result
        let type = result.type
        wx.setStorageSync('token', result.token)
        app.globalData.type = type
        if (result.type){
          wx.showToast({
            title: '登录成功！',
            icon:'none',
            duration:500
          })
          wx.setStorageSync('username', username)
          if (result.type == 3) {
            app.globalData.name = res.data.result.merchant.name
            setTimeout(() => {
              wx.redirectTo({
                url: '../transaction/index?type='+ type,
              })
            }, 700)
          } else {
            app.globalData.name = res.data.result.store.name
            setTimeout(() => {
              wx.redirectTo({
                url: '../transaction_01/transaction_01?username=' + username + '&type=' + type,
              })
            }, 700)
          }
        
        }else{
          wx.showToast({
            title: '暂无权限登陆！',
            icon: 'none',
            duration: 500
          })
        }
      }else{
        wx.showToast({
          title: res.data.message,
          icon:'none'
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      username: wx.getStorageSync("username")
    })
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})